using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
namespace sort
{

    [TestFixture]
    public class sorterTestSuite
    {
        List<string> Bigtxt = new List<string>();
        List<S> BigT = new List<S>();

        List<string> Smalltxt = new List<string>();
        List<S> SmallT = new List<S>();

        public sorterTestSuite()
        {
            for (int i = 2; i < 40; i += 2) { SmallT.Add((new qsort(i)).Quick2v); Smalltxt.Add("quick" + i); }
            for (int i = 2; i < 40; i += 2) { BigT.Add((new qsort(i)).Quick2v); Bigtxt.Add("quick" + i); }
        }

        void valid(int[] A)
        {
            for (int i = 1; i < A.Length; i++)
            {
                //                Console.WriteLine("{0}", A[i]);
                if (A[i] < A[i - 1])
                    Assert.GreaterOrEqual(A[i], A[i - 1], "wykryto nie mniejsze  " + i);
            }
        }


        delegate void S(int[] a);
        int[] generate(int seed, int size)
        {
            Random r = new Random(seed);
            int[] A = new int[size];
            for (int i = 0; i < A.Length; i++) A[i] = r.Next();
            return A;
        }
        int[] generateSrt(int seed, int size)
        {
            Random r = new Random(seed);
            int[] A = new int[size];
            for (int i = 0; i < A.Length; i++) A[i] = i + r.Next() % 5;
            return A;
        }
        int[] generateRvs(int seed, int size)
        {
            Random r = new Random(seed);
            int[] A = new int[size];
            for (int i = 0; i < A.Length; i++) A[i] = size - i + r.Next() % 5;
            return A;
        }
        int[] generateConst(int seed, int size)
        {
            int[] A = new int[size];
            for (int i = 0; i < A.Length; i++) A[i] = seed;
            return A;
        }
        delegate int[] TabGenerator(int seed, int size);
        double dosmalltest(int seed, int size, S srt)
        {
            int n = 100000;
            int dziel = size / 20 + 1;
            n /= dziel;
            int[][] Tab = new int[n][];
            for (int i = 0; i < n; i++) Tab[i] = generate(seed + i, size);
            Timer t = new Timer();
            t.Start();
            for (int i = 0; i < n; i++) srt(Tab[i]);
            t.Stop();
            for (int i = 0; i < n; i++) valid(Tab[i]);
            return t.Duration * dziel;
            //return ((TimeSpan)(DateTime.Now - start)).Ticks;
        }




        [Test]//old
        public void SmallDataSpeed()
        {

            Console.Write("   ");
            foreach (string t in Smalltxt)
            {
                Console.Write("           " + t);
            }
            Console.Write("\n");
            for (int i = 1; i < 100; i += 2)
            {
                Console.Write(i.ToString("D6"));
                foreach (S srt in SmallT)
                {
                    Console.Write("      " + dosmalltest(0, i, srt).ToString("F6"));
                }
                Console.Write("\n");
            }
        }

        double DoBigTest(int seed, int size, S srt, TabGenerator gen)
        {
            int n = 10;
            int[] Tab;
            Timer t = new Timer();
            double dur = 0;
            for (int i = 0; i < n; i++)
            {
                Tab = generate(seed + i, size);
                t.Start();
                srt(Tab);
                t.Stop();
                valid(Tab);
                dur += t.Duration;
            }
            return dur;

        }


        [Test]
        public void BigDataSpeed()
        {
            Console.Write("big       ");
            foreach (string t in Bigtxt)
            {
                Console.Write(t + "      ");
            }
            Console.Write("\n");
            for (int i = 1; i < 10000000; i *= 2)
            {
                Console.Write(i.ToString("D8"));
                foreach (S srt in BigT)
                {
                    Console.Write("   " + DoBigTest(0, i, srt, generate).ToString("F6"));
                }
                Console.Write("\n");
            }
        }
        [Test]
        public void BigDataSpeedAlmSrt()
        {

            Console.Write("2   ");
            foreach (string t in Bigtxt)
            {
                Console.Write(t + "      ");
            }
            Console.Write("\n");
            for (int i = 1; i < 10000000; i *= 2)
            {
                Console.Write(i.ToString("D8"));
                foreach (S srt in BigT)
                {
                    Console.Write("   " + DoBigTest(0, i, srt, generateSrt).ToString("F6"));
                }
                Console.Write("\n");
            }
        }
        [Test]
        public void BigDataSpeedReverseSrt()
        {

            Console.Write("3   ");
            foreach (string t in Bigtxt)
            {
                Console.Write(t + "      ");
            }
            Console.Write("\n");
            for (int i = 1; i < 10000000; i *= 2)
            {
                Console.Write(i.ToString("D8"));
                foreach (S srt in BigT)
                {
                    Console.Write("   " + DoBigTest(0, i, srt, generateRvs).ToString("F6"));
                }
                Console.Write("\n");
            }
        }
        [Test]
        public void BigDataSpeedConst()
        {

            Console.Write("4   ");

            foreach (string t in Bigtxt)
            {
                Console.Write(t + "      ");
            }
            Console.Write("\n");
            for (int i = 1; i < 10000000; i *= 2)
            {
                Console.Write(i.ToString("D8"));
                foreach (S srt in BigT)
                {
                    Console.Write("   " + DoBigTest(0, i, srt, generateRvs).ToString("F6"));
                }
                Console.Write("\n");
            }
        }



        [Test]
        [Explicit]
        public void CorrectBigData()
        {
            for (int s = 0; s < 100000; s++)
            {
                Console.Write("seed" + s + "       ");
                foreach (string t in Bigtxt)
                {
                    Console.Write(t + "      ");
                }
                Console.Write("\n");
                for (int i = 1; i < 10000000; i *= 2)
                {
                    Console.Write(i.ToString("D8"));
                    foreach (S srt in BigT)
                    {
                        Console.Write("   " + DoBigTest(s, i, srt, generate).ToString("F6"));
                    }
                    Console.Write("\n");
                }
            }
        }
    }



}
